from flask import Flask
from flask_log import *


def create_flask_app(config):
    """
    创建Flask应用
    :param config: 配置信息对象
    :return: Flask应用
    """
    app = Flask(__name__)
    app.config.from_object(config)
    return app


def create_app(config):
    """
    app工厂函数
    :param config: 配置对象
    :return: app
    """
    app = create_flask_app(config)

    # 配置日志
    app.logger = logging.getLogger('flask')

    # redis客户端
    from redis import StrictRedis
    app.redis_client = StrictRedis()

    # 微信客户端工具
    from wechatpy import WeChatClient
    from .utils import WeChatStorage
    app.wechat_client = WeChatClient(app.config['WECHAT_APPID'], app.config['WECHAT_SCRETE'],
                                     session=WeChatStorage())

    # MySQL数据库连接初始化
    from app.models import db
    # 等app创建好之后，再给db数据库对象进行真实初始化操作
    db.init_app(app)


    return app


